<?php

declare(strict_types=1);

namespace app\admin\service\dashboard;

use app\api\lib\SuPengJun;
use app\common\model\dynamic\DynamicCommentModel;
use app\common\model\dynamic\DynamicModel;
use app\common\model\order\OrderModel;
use app\common\model\order\OrderRefundModel;
use app\common\model\system\PluginModel;
use app\common\model\user\UserCartModel;
use app\common\model\user\UserModel;
use app\common\model\user\UserPrivacyModel;
use Dengje\Pxy\BaseService;
use UserCart;

class DashboardService extends BaseService
{
    public function header()
    {
        $newDays = strtotime(date("Y-m-d", time()));
        $oneDays = strtotime(date('Y-m-d', strtotime('1 days ago')));
        $condition1[] = ['create_time', '>=', $newDays];
        $condition3[] = ['type', '=', 0];
        $condition3[] = ['pay_status', 'in', [1, 2, 3]];
        $price = OrderModel::where($condition1)->where($condition3)->sum('can_refund_price');
        $order = OrderModel::where($condition1)->where('type', 0)->count();
        $cart = UserCartModel::where($condition1)->count();
        $orderRefund = OrderRefundModel::where($condition1)->whereIn('refund_type', [0, 1])->count();
        $condition2[] = ['create_time', '<', $newDays];
        $condition2[] = ['create_time', '>=', $oneDays];
        $pricePrevious = OrderModel::where($condition2)->where($condition3)->sum('can_refund_price');
        $orderPrevious = OrderModel::where($condition2)->where('type', 0)->count();
        $cartPrevious = UserCartModel::where($condition2)->count();
        $orderRefundPrevious = OrderRefundModel::where($condition2)->whereIn('refund_type', [0, 1])->count();
        $data[0] = ['title' => '今日订单收益', 'num' => $price, 'proportion' => SuPengJun::calculateOnGrowth($price, $pricePrevious)];
        $data[1] = ['title' => '今日订单数', 'num' => $order, 'proportion' => SuPengJun::calculateOnGrowth($price, $orderPrevious)];
        $data[2] = ['title' => '今日加购数', 'num' => $cart, 'proportion' => SuPengJun::calculateOnGrowth($price, $cartPrevious)];
        $data[3] = ['title' => '今日售后数', 'num' => $orderRefund, 'proportion' => SuPengJun::calculateOnGrowth($price, $orderRefundPrevious)];
        return $data;
    }

    public function center($data)
    {
        $data = $this->preExcludeFields($data);
        if ($data['type'] == 1) {
            $condition[] = ['create_time', '>=', strtotime(date('Y-m-d', strtotime('7 days ago')))];
        } else if ($data['type'] == 2) {
            $condition[] = ['create_time', '>=', strtotime(date('Y-m-d', strtotime('1 months ago')))];
        } else if ($data['type'] == 3) {
            $condition[] = ['create_time', '>=', strtotime(date('Y-m-d', strtotime('6 months ago')))];
        } else if ($data['type'] == 4) {
            $condition[] = ['create_time', '>=', strtotime(date('Y-m-d', strtotime('1 years ago')))];
        }
        $condition[] = ['status', '<>', 0];
        $dynamicData['count'] = DynamicModel::where($condition)->count();
        $type1 = DynamicModel::where($condition)->where('type', 0)->count();
        $type2 = DynamicModel::where($condition)->where('type', 1)->count();
        $type3 = DynamicModel::where($condition)->where('type', 2)->count();
        $type4 = DynamicModel::where($condition)->where('type', 3)->count();
        $dynamicData['type1'] = $type1 && $dynamicData['count'] ? round($type1 / $dynamicData['count'] * 100, 2) : 0;
        $dynamicData['type2'] = $type2 && $dynamicData['count'] ? round($type2 / $dynamicData['count'] * 100, 2) : 0;
        $dynamicData['type3'] = $type3 && $dynamicData['count'] ? round($type3 / $dynamicData['count'] * 100, 2) : 0;
        $dynamicData['type4'] = $type4 && $dynamicData['count'] ? round($type4 / $dynamicData['count'] * 100, 2) : 0;
        return $dynamicData;
    }

    public function notice()
    {
        $data[0] = [
            'title' => '用户审核',
            'url' => 'user/privacy/index',
            'idx' => 1,
            'count' => UserPrivacyModel::where('examine', 1)->count()
        ];
        $data[1] = [
            'title' => '笔记审核',
            'url' => 'dynamic/list/index',
            'idx' => 2,
            'count' => DynamicModel::where('status', 1)->count()
        ];
        $data[2] = [
            'title' => '评论审核',
            'url' => 'dynamic/list/index',
            'idx' => 2,
            'count' => DynamicCommentModel::where('status', 0)->count()
        ];
        $data[3] = [
            'title' => '订单发货',
            'url' => 'shop/order/index',
            'idx' => 3,
            'count' => OrderModel::where('status', 2)->where('type', 0)->count()
        ];
        $data[4] = [
            'title' => '订单售后',
            'url' => 'shop/order/index',
            'idx' => 3,
            'count' => OrderRefundModel::where('status', 0)->whereIn('refund_type', [0, 1])->count()
        ];
        $activity = PluginModel::where('name', 'activity')->value('id');
        if ($activity) {
            $data[5] = [
                'title' => '活动售后',
                'url' => 'activity/user/index',
                'idx' => 3,
                'count' => OrderRefundModel::where('status', 0)->where('refund_type', 2)->count()
            ];
        }
        return $data;
    }

    public function user()
    {
        $where1[] = ['status', '=', 1];
        $where2[] = ['status', '=', 8];
        $user1 = UserModel::where($where1)->count();
        $user2 = UserModel::where($where2)->count();
        $vipCount = $user1 + $user2;
        $data['user'] = [
            $user1 ? round($user1 / $vipCount * 100, 2) : 0,
            $user2 ? round($user2 / $vipCount * 100, 2) : 0,
        ];
        $condition[] = ['status', '=', 1];
        $gender1 = UserModel::where($condition)->where('gender', 0)->count();
        $gender2 = UserModel::where($condition)->where('gender', 1)->count();
        $gender3 = UserModel::where($condition)->where('gender', 2)->count();
        $userCount = $gender1 + $gender2 + $gender3;
        $data['gender'] = [
            $gender1 ? round($gender1 / $userCount * 100, 2) : 0,
            $gender2 ? round($gender2 / $userCount * 100, 2) : 0,
            $gender3 ? round($gender3 / $userCount * 100, 2) : 0
        ];
        $ages = UserModel::where($condition)->where('age', '<>', '')->where('age', '<>', '暂不展示')->distinct()->column('age');
        $ageUnknown = 100;
        foreach ($ages as $item) {
            $ageCount = UserModel::where($condition)->where('age', $item)->count();
            $proportion = $ageCount ? round($ageCount / $userCount * 100, 2) : 0;
            $ageList[] = ['title' => $item, 'proportion' => $proportion];
            $ageUnknown = round($ageUnknown - $proportion, 2);
        }
        if ($ageUnknown) $ageList[] = ['title' => '未知', 'proportion' => $ageUnknown];
        $data['age'] = $ageList;
        $citys = UserModel::where($condition)->where('city', '<>', '')->distinct()->column('city');
        $cityUnknown = 100;
        foreach ($citys as $item) {
            $cityCount = UserModel::where($condition)->where('city', $item)->count();
            $proportion = $cityCount ? round($cityCount / $userCount * 100, 2) : 0;
            $cityList[] = ['title' => $item . ' ' . $cityCount . '人 | ', 'proportion' => $proportion];
            $cityUnknown = round($cityUnknown - $proportion, 2);
        }
        if ($cityUnknown) $cityList[] = ['title' => '未知 ', 'proportion' => $cityUnknown];
        $data['city'] = $cityList;
        return $data;
    }

    public function activity()
    {
        $newDays = strtotime(date("Y-m-d", time()));
        $oneDays = strtotime(date('Y-m-d', strtotime('1 days ago')));
        $condition1[] = ['create_time', '>=', $newDays];
        $condition1[] = ['type', '=', 1];
        $price = OrderModel::where($condition1)->sum('can_refund_price');
        $condition2[] = ['create_time', '<', $newDays];
        $condition2[] = ['create_time', '>=', $oneDays];
        $condition2[] = ['type', '=', 1];
        $condition2[] = ['pay_status', 'in', [1, 2, 3]];
        $pricePrevious = OrderModel::where($condition2)->sum('can_refund_price');
        $data[0] = ['title' => '今日活动收益', 'num' => $price, 'proportion' => SuPengJun::calculateOnGrowth($price, $pricePrevious)];
        return $data;
    }
}
